package org.example.util;

import org.example.domain.ArriveorderVo;
import org.example.domain.PoArriveorderVo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/* 4. 优化后的分类器，单次遍历即可 */
public final class OptimizedClassifier {

    /* 演示 */
    public static void main(String[] args) {
        List<PoArriveorderVo> list = new ArrayList<>();
        PoArriveorderVo vo1 = new PoArriveorderVo();
        vo1.setIndex("1");
        vo1.setId("ZSYS15052025040300001");
        vo1.setWeighingNo(" GB3WN4250331151437MR");
        vo1.setSupplierName("呼伦⻉尔市华亿物资销售有限公司");
        list.add(vo1);

        // 创建第二个 PoArriveorderVo 对象
        PoArriveorderVo vo2 = new PoArriveorderVo();
        vo2.setIndex("2");
        vo2.setId("ZSYS15052025040300001");
        vo2.setWeighingNo("ZSYS150520250403009955");
        vo2.setSupplierName("呼伦⻉尔市华亿物资销售有限公司");
        list.add(vo2);

        // 创建第二个 PoArriveorderVo 对象
        PoArriveorderVo vo3 = new PoArriveorderVo();
        vo3.setIndex("3");
        vo3.setId("ZSYS15052025040300001");
        vo3.setWeighingNo("ZSYS150520250403009955");
        vo3.setSupplierName("呼伦⻉尔市华亿物资销售有限公司");
        list.add(vo3);

        // 创建第二个 PoArriveorderVo 对象
        PoArriveorderVo vo4 = new PoArriveorderVo();
        vo4.setIndex("2");
        vo4.setId("ZSYS15052025040300001");
        vo4.setWeighingNo("ZSYS150520250403009955");
        vo4.setSupplierName("呼伦⻉尔市华亿物资销售有限公司");
        list.add(vo4);

        // 创建第二个 PoArriveorderVo 对象
        PoArriveorderVo vo5 = new PoArriveorderVo();
        vo5.setIndex("1");
        vo5.setId("ZSYS15052025040300001");
        vo5.setWeighingNo("ZSYS150520250403009955");
        vo5.setSupplierName("呼伦⻉尔市华亿物资销售有限公司");
        list.add(vo5);

        List<ArriveorderVo> collect = list.stream().collect(Collectors.groupingBy(PoArriveorderVo::getIndex))
                .entrySet()
                .stream()
                .map(e -> {
                    ArriveorderVo arriveorderVo = new ArriveorderVo();
                    String key = e.getKey();
                    List<PoArriveorderVo> value = e.getValue();
                    arriveorderVo.setIndex(key);
                    arriveorderVo.setOrderVoList(value);
                    return arriveorderVo;
                }).collect(Collectors.toList());
        System.out.println(collect);
        
        Map<String, List<PoArriveorderVo>> poArriveorderVoMap = new HashMap<>();
        for (PoArriveorderVo poArriveorderVo : list) {
            String index = poArriveorderVo.getIndex();
            if (null == poArriveorderVoMap.get(index)) {
                List<PoArriveorderVo> arriveorderVos = new ArrayList<>();
                arriveorderVos.add(poArriveorderVo);
                poArriveorderVoMap.put(index, arriveorderVos);
            } else {
                List<PoArriveorderVo> arriveorderVos1 = poArriveorderVoMap.get(index);
                arriveorderVos1.add(poArriveorderVo);
                poArriveorderVoMap.put(index, arriveorderVos1);
            }
        }
        List<ArriveorderVo> collect1 = poArriveorderVoMap.entrySet().stream().map(e -> {
            ArriveorderVo arriveorderVo = new ArriveorderVo();
            String key = e.getKey();
            List<PoArriveorderVo> value = e.getValue();
            arriveorderVo.setIndex(key);
            arriveorderVo.setOrderVoList(value);
            return arriveorderVo;
        }).collect(Collectors.toList());
        System.out.println(collect1);
    }

}